/*
 * idView.java
 *
 * Created on Mar 21, 2010, 6:53:41 PM
 */
package assetl.desktop;

import assetl.system.User;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import assetl.system.AssetLControl;
import assetl.system.DataPacket;
import assetl.system.StringPacket;

/**
 * Prompts for I-number or Laptop number (if they are an admin)
 *
 * @author Devin Doman
 */
public class IDView
   extends AssetView
{
   /**
    * Constructor for IDView
    *
    * @param pControl The controller for this view
    */
   public IDView(AssetLControl pControl)
   {
      super(pControl);
      initComponents();
      mGroup = new ButtonGroup();
      mGroup.add(mScheduleRadio);
      mGroup.add(mCheckInRadio);

      // Add listeners

      mScheduleRadio.addActionListener(new ActionListener()
      {
         /**
          * Enable the ability to load a person in
          * the next view
          *
          * @param ev The event
          */
         public void actionPerformed(ActionEvent ev)
         {
            enable("LoadPerson");
         }
      });

      mCheckInRadio.addActionListener(new ActionListener()
      {
         /**
          * Enable the ability to checkin a laptop
          *
          * @param ev The event
          */
         public void actionPerformed(ActionEvent ev)
         {
            enable("Checkin");
         }
      });
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      mTxtFld = new javax.swing.JTextField();
      mScheduleRadio = new javax.swing.JRadioButton();
      mCheckInRadio = new javax.swing.JRadioButton();
      mBtn = new javax.swing.JButton();
      mLabel = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      mTxtFld.setName("mTxtFld"); // NOI18N
      mTxtFld.addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyPressed(java.awt.event.KeyEvent evt) {
            textFldKeyPressed(evt);
         }
      });

      mScheduleRadio.setText("Schedule");
      mScheduleRadio.setName("mScheduleRadio"); // NOI18N

      mCheckInRadio.setText("Check In");
      mCheckInRadio.setName("mCheckInRadio"); // NOI18N

      mBtn.setText("Execute");
      mBtn.setName("mBtn"); // NOI18N

      mLabel.setText("Enter");
      mLabel.setName("mLabel"); // NOI18N

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(23, 23, 23)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(mScheduleRadio)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(mCheckInRadio)
                  .addGap(61, 61, 61)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(mTxtFld, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                     .addComponent(mBtn)
                     .addComponent(mLabel))))
            .addContainerGap(140, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addGap(84, 84, 84)
                  .addComponent(mLabel)
                  .addGap(31, 31, 31)
                  .addComponent(mTxtFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(56, 56, 56)
                  .addComponent(mBtn))
               .addGroup(layout.createSequentialGroup()
                  .addGap(108, 108, 108)
                  .addComponent(mScheduleRadio)
                  .addGap(18, 18, 18)
                  .addComponent(mCheckInRadio)))
            .addContainerGap(72, Short.MAX_VALUE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

   private void textFldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_textFldKeyPressed
   {//GEN-HEADEREND:event_textFldKeyPressed
      //If enter is pushed from the text field press the button
      if (evt.getKeyCode() == KeyEvent.VK_ENTER)
      {
         mBtn.doClick();
      }
   }//GEN-LAST:event_textFldKeyPressed
   // Variables declaration - do not modify//GEN-BEGIN:variables
   /**
   * Button to perform current function
   */
   private javax.swing.JButton mBtn;
   /**
   * Changes to Checkin function
   */
   private javax.swing.JRadioButton mCheckInRadio;
   /**
   * Label for text field
   */
   private javax.swing.JLabel mLabel;
   /**
   * Changes to Schedule function
   */
   private javax.swing.JRadioButton mScheduleRadio;
   /**
   * Text field to enter data
   */
   private javax.swing.JTextField mTxtFld;
   // End of variables declaration//GEN-END:variables
   /**
    * Groups the radio buttons so only one is selected
    */
   private ButtonGroup mGroup;

   /**
    * Sets what mode the view is functioning in
    *
    * @param pIsLoad True if the view is performing person loading function
    */
   public void setMode(boolean pIsLoad)
   {
      mScheduleRadio.setSelected(pIsLoad);
      mCheckInRadio.setSelected(!pIsLoad);
      mTxtFld.setText("");
      mTxtFld.requestFocus();

      if (pIsLoad)
      {
         setLabeling("Load", "Enter I-number:");
      }
      else
      {
         setLabeling("Checkin", "Enter laptop barcode:");
      }
   }

   /**
    * Changes the button label and the label above the text field
    *
    * @param pBtnLbl The new label for the button
    * @param pLbl The new label for the text field
    */
   public void setLabeling(String pBtnLbl, String pLbl)
   {
      mLabel.setText(pLbl);
      setLabel(pBtnLbl, mBtn);
   }

   /**
    * Grabs all pertainent data from the fields and sets them in
    * a DataPacket object. This DataPacket is then returned by the
    * method.
    *
    * @param pFunction The function that needs the DataPacket
    * @return The DataPacket that has been set
    */
   public DataPacket grabDataPacket(String pFunction)
   {
      //Send the string in a packet to the controller
      mPacket = new StringPacket(mTxtFld.getText());
      return mPacket;
   }

   /**
    * Allows control of the visibility of Admin components in the view
    *
    * @param pIsAdmin True if the admin components are to be set to visible
    */
   @Override
   public void setAdminComponents(int pIsAdmin)
   {
      super.setAdminComponents(pIsAdmin);
      mCheckInRadio.setVisible(pIsAdmin <= User.GATEKEEPER);
      mScheduleRadio.setVisible(pIsAdmin <= User.GATEKEEPER);
   }

   /**
    * Gets any needed data from the model
    */
   public void updateData()
   {
      // There is nothing to update for this view
   }

   /**
    * Enables functionality passed in for this view.
    */
   public void enable(String pFunction)
   {
      //Enable a FunctionListener for the button
      enable(pFunction, mBtn, "Function");
      setMode("LoadPerson".equals(pFunction));
   }

   /**
    * The entry point for this view
    */
   public void run()
   {
   }
}
